﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using LuaInterface;

public class ToluaTest02 : MonoBehaviour {

	// Use this for initialization
	void Start () {

        LuaState state = new LuaState();
        state.Start();

        //执行lua绝对目录
        //string path = Application.dataPath + "/Scripts/mylua.lua";
        //state.DoFile(path);

        //添加lua的搜索目录
        state.AddSearchPath(Application.dataPath 
            + "/Scripts/");
        state.DoFile("testlua02.lua");

        //C#执行lua的函数
        LuaFunction func = 
            state.GetFunction("tb.Test");
        var result = func.Call(6);
        //C#给lua传参，lua返回值给C#
        Debug.Log(result[0]+":"+ result[1]);

        //C#获取lua的变量
        state["name"] = "你是C#的名字";
        Debug.Log(state["name"]);

        LuaTable tb = state.GetTable("tb");
        LuaFunction tb_func = tb.GetLuaFunction("Test");
        tb_func.Call(6);

        Debug.Log(tb["age"]);
        Debug.Log(state["a"]);

        //获取元表
        LuaTable mt = tb.GetMetaTable();
        Debug.Log(mt["name"]);

        //lua Table->C# Attay
        LuaTable data = state.GetTable("data");
        object[] array = data.ToArray();
        foreach (var item in array)
        {
            Debug.Log(item);
        }
    }

    // Update is called once per frame
    void Update () {
		
	}
}
